package com.zhima.panel;

import cn.hutool.core.io.resource.ResourceUtil;
import com.zhima.data.ArrayDimension;
import com.zhima.data.RandomDataType;
import com.zhima.function.ConfigurationIndexAssociate;
import com.zhima.function.RemoveSelectedRow;

import javax.swing.*;
import java.awt.*;

import static com.zhima.data.RandomDataType.ARRAY;
import static com.zhima.data.RandomDataType.BOOLEAN;
import static com.zhima.util.StrUtil.addUnderLine;
import static com.zhima.util.StrUtil.removeRedundantUnderline;
import static javax.swing.JOptionPane.QUESTION_MESSAGE;


public class RandomInDataConfiguration extends JPanel {
    private final JComboBox<RandomDataType> randomDataTypeJComboBox = new JComboBox<>(RandomDataType.values());
    private final JComboBox<ArrayDimension> arrayDimensionJComboBox = new JComboBox<>(ArrayDimension.values());
    private final JComboBox<RandomDataType> arrayElementRandomDataTypeJComboBox = new JComboBox<>(RandomDataType.values());
    private JLabel arrayElementJLabel;
    private JLabel arrayElementResultJLabel;
    protected final RemoveSelectedRow removeSelectedRow;
    protected final ConfigurationIndexAssociate configurationIndexAssociate;
    protected int rowIndex;
    protected JLabel rowInfoJLabel;
    private JLabel randomDataTypeResultJLabel;
    private JLabel minJLabel;
    private JLabel minResultJLabel;
    private JTextField minJTextField;
    private JLabel maxJLabel;
    private JLabel maxResultJLabel;
    private JTextField maxJTextField;
    private boolean initArrayComponents;
    private JLabel arrayDimensionJLabel;
    private JLabel arrayDimensionResultJLabel;

    private JLabel oneDimensionMaxLengthJLabel;
    private JTextField oneDimensionMaxLengthJTextField;
    private JLabel oneDimensionMaxLengthResultJLabel;
    private JLabel oneDimensionMinLengthJLabel;
    private JTextField oneDimensionMinLengthJTextField;
    private JLabel oneDimensionMinLengthResultJLabel;

    private JLabel twoDimensionMinLengthJLabel;
    private JTextField twoDimensionMinLengthJTextField;
    private JLabel twoDimensionMinLengthResultJLabel;
    private JLabel twoDimensionMaxLengthJLabel;
    private JTextField twoDimensionMaxLengthJTextField;
    private JLabel twoDimensionMaxLengthResultJLabel;

    private JLabel printLengthJLabel;
    private JCheckBox printLengthJCheckBox;
    private JButton editJButton;
    private JButton confirmJButton;
    protected JButton deleteJButton;
    protected JButton dataAssociationJButton;
    protected JButton cancelDataAssociationJButton;
    protected JLabel dataAssociationJLabel;
    protected JLabel dataAssociationResultJLabel;

    public RandomInDataConfiguration(RemoveSelectedRow removeSelectedRow, ConfigurationIndexAssociate configurationIndexAssociate) {
        this.removeSelectedRow = removeSelectedRow;
        this.configurationIndexAssociate = configurationIndexAssociate;
    }

    public RandomInDataConfiguration(int rowIndex, RemoveSelectedRow removeSelectedRow, ConfigurationIndexAssociate configurationIndexAssociate) {
        setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
        this.removeSelectedRow = removeSelectedRow;
        this.rowIndex = rowIndex;
        this.configurationIndexAssociate = configurationIndexAssociate;
        this.arrayElementRandomDataTypeJComboBox.removeItem(RandomDataType.ARRAY);
        initComponents();
        initActionListener();
        initColor();
        toEdited();
    }

    private void initColor() {
        Color red = Color.RED;
        this.randomDataTypeResultJLabel.setForeground(red);
        this.minResultJLabel.setForeground(red);
        this.maxResultJLabel.setForeground(red);
        if (initArrayComponents) {
            this.arrayDimensionResultJLabel.setForeground(red);
            this.arrayElementResultJLabel.setForeground(red);
            this.oneDimensionMinLengthResultJLabel.setForeground(red);
            this.oneDimensionMaxLengthResultJLabel.setForeground(red);
            this.twoDimensionMinLengthResultJLabel.setForeground(red);
            this.twoDimensionMaxLengthResultJLabel.setForeground(red);
        }
    }

    private void initActionListener() {
        randomDataTypeJComboBox.addActionListener(e -> {
            JComboBox<RandomDataType> source = (JComboBox<RandomDataType>) e.getSource();
            Object selectedRandomType = source.getSelectedItem();
            if (selectedRandomType == RandomDataType.ARRAY) {
                initArrayComponents();
                controlMinMaxComponents(arrayElementRandomDataTypeJComboBox.getSelectedItem() != BOOLEAN);
                arrayDimensionJComboBox.setSelectedItem(ArrayDimension.valueOf(arrayDimensionResultJLabel.getText()));
            } else {
                hideAllArrayComponents();
                if (selectedRandomType == BOOLEAN) {
                    controlMinMaxComponents(false);
                } else {
                    controlMinMaxComponents(true);
                }
            }
            randomDataTypeResultJLabel.setText(((RandomDataType) selectedRandomType).name());
            configurationIndexAssociate.accept(this);
            revalidate();
        });
        editJButton.addActionListener(e -> toEdited());
        confirmJButton.addActionListener(e -> confirmConfiguration());
        deleteJButton.addActionListener(e -> removeSelectedRow.accept(this.rowIndex));
        initDataAssociationActionListener();
        arrayDimensionJComboBox.addActionListener(e -> {
            JComboBox<ArrayDimension> source = (JComboBox<ArrayDimension>) e.getSource();
            Object selectedArrayDimension = source.getSelectedItem();
            arrayDimensionResultJLabel.setText(((ArrayDimension) selectedArrayDimension).name());
            if (selectedArrayDimension == ArrayDimension.ONE) {
                controlTwoDimensionLengthComponents(false);
            } else {
                controlTwoDimensionLengthComponents(true);
                twoDimensionMinLengthResultJLabel.setVisible(false);
                twoDimensionMaxLengthResultJLabel.setVisible(false);
            }
        });
        arrayElementRandomDataTypeJComboBox.addActionListener(e -> {
            JComboBox<RandomDataType> source = (JComboBox<RandomDataType>) e.getSource();
            arrayElementResultJLabel.setText(((RandomDataType) source.getSelectedItem()).name());
            controlMinMaxComponents(source.getSelectedItem() != BOOLEAN);
        });
    }

    private void controlMinMaxComponents(boolean show) {
        minJLabel.setVisible(show);
        minJTextField.setVisible(show);
        maxJLabel.setVisible(show);
        maxJTextField.setVisible(show);
    }

    protected void initDataAssociationComponents() {
        this.dataAssociationJButton = new JButton(getImageIcon("imgs/add.png", 27, 27));
        this.dataAssociationJButton.setPreferredSize(new Dimension(27, 27));
        this.dataAssociationJButton.setToolTipText("添加数据关联");
        this.cancelDataAssociationJButton = new JButton(getImageIcon("imgs/cancel.png", 27, 27));
        this.cancelDataAssociationJButton.setPreferredSize(new Dimension(27, 27));
        this.cancelDataAssociationJButton.setToolTipText("取消数据关联");
        this.dataAssociationJLabel = new JLabel("数据关联：");
        this.dataAssociationResultJLabel = new JLabel("无");
        this.dataAssociationResultJLabel.setForeground(Color.RED);
    }

    public static ImageIcon getImageIcon(String fileName, int width, int height) {
        ImageIcon imageIcon = new ImageIcon(ResourceUtil.getResource(fileName));
        imageIcon.setImage(imageIcon.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT));
        return imageIcon;
    }

    private void controlArrayDimensionComponents(boolean show) {
        arrayDimensionResultJLabel.setVisible(show);
        arrayDimensionJLabel.setVisible(show);
        arrayDimensionJComboBox.setVisible(show);
    }

    private void hideElementRandomDataTypeComponents(boolean show) {
        arrayElementJLabel.setVisible(show);
        arrayElementResultJLabel.setVisible(show);
        arrayElementRandomDataTypeJComboBox.setVisible(show);
    }

    private void hideAllArrayComponents() {
        if (!initArrayComponents) {
            return;
        }
        controlArrayDimensionComponents(false);
        hideElementRandomDataTypeComponents(false);

        controlOneDimensionLengthComponents(false);
        controlTwoDimensionLengthComponents(false);

        controlArrayLengthComponents(false);
    }

    private void controlArrayLengthComponents(boolean show) {
        printLengthJLabel.setVisible(show);
        printLengthJCheckBox.setVisible(show);
    }

    private void controlOneDimensionLengthComponents(boolean show) {
        // 最小长度
        oneDimensionMinLengthJLabel.setVisible(show);
        // 最小长度的值
        oneDimensionMinLengthResultJLabel.setVisible(show);
        // 最小长度的输入框
        oneDimensionMinLengthJTextField.setVisible(show);

        oneDimensionMaxLengthJLabel.setVisible(show);
        oneDimensionMaxLengthResultJLabel.setVisible(show);
        oneDimensionMaxLengthJTextField.setVisible(show);
    }

    private void controlTwoDimensionLengthComponents(boolean show) {
        twoDimensionMinLengthJLabel.setVisible(show);
        twoDimensionMinLengthResultJLabel.setVisible(show);
        twoDimensionMinLengthJTextField.setVisible(show);
        twoDimensionMaxLengthJLabel.setVisible(show);
        twoDimensionMaxLengthResultJLabel.setVisible(show);
        twoDimensionMaxLengthJTextField.setVisible(show);
    }

    private void initArrayComponents() {
        if (!initArrayComponents) {
            this.arrayDimensionJLabel = new JLabel("维度：");
            this.arrayDimensionResultJLabel = new JLabel(ArrayDimension.ONE.name());
            this.arrayElementJLabel = new JLabel("元素类型：");
            this.arrayElementResultJLabel = new JLabel(RandomDataType.INT.name());

            this.oneDimensionMinLengthJLabel = new JLabel("长度最小值：");
            this.oneDimensionMinLengthJTextField = new JTextField("5", 10);
            this.oneDimensionMinLengthResultJLabel = new JLabel("5");

            this.oneDimensionMaxLengthJLabel = new JLabel("长度最大值：");
            this.oneDimensionMaxLengthJTextField = new JTextField("200", 10);
            this.oneDimensionMaxLengthResultJLabel = new JLabel("200");

            this.twoDimensionMinLengthJLabel = new JLabel("二维长度最小值：");
            this.twoDimensionMinLengthJTextField = new JTextField("5", 10);
            this.twoDimensionMinLengthResultJLabel = new JLabel("5");

            this.twoDimensionMaxLengthJLabel = new JLabel("二维长度最大值：");
            this.twoDimensionMaxLengthJTextField = new JTextField("200", 10);
            this.twoDimensionMaxLengthResultJLabel = new JLabel("200");

            this.arrayDimensionJComboBox.setSelectedIndex(0);
            this.printLengthJLabel = new JLabel("数组长度：");
            this.printLengthJCheckBox = new JCheckBox();
            initArrayComponents = true;
            initColor();
            remove(editJButton);
            remove(confirmJButton);
            remove(deleteJButton);
            removeDataAssociationComponents();
            removeMinMaxComponents();

            addArrayDimensionComponents();
            addArrayElementRandomDataTypeComponents();

            addMinMaxComponents();
            addOneDimensionLengthComponents();
            addTwoDimensionLengthComponents();

            add(printLengthJLabel);
            add(printLengthJCheckBox);
            add(editJButton);
            add(confirmJButton);
            add(deleteJButton);
            addDataAssociationComponents();
        }
        arrayElementJLabel.setVisible(true);
        arrayElementRandomDataTypeJComboBox.setVisible(true);
        arrayElementResultJLabel.setVisible(false);

        arrayDimensionJLabel.setVisible(true);
        arrayDimensionJComboBox.setVisible(true);
        arrayDimensionResultJLabel.setVisible(false);

        oneDimensionMinLengthJLabel.setVisible(true);
        oneDimensionMinLengthJTextField.setVisible(true);
        oneDimensionMinLengthResultJLabel.setVisible(false);
        oneDimensionMaxLengthJLabel.setVisible(true);
        oneDimensionMaxLengthJTextField.setVisible(true);
        oneDimensionMaxLengthResultJLabel.setVisible(false);
    }

    private void addArrayElementRandomDataTypeComponents() {
        add(arrayElementJLabel);
        add(arrayElementRandomDataTypeJComboBox);
        add(arrayElementResultJLabel);
    }

    private void addArrayDimensionComponents() {
        add(arrayDimensionJLabel);
        add(arrayDimensionJComboBox);
        add(arrayDimensionResultJLabel);
    }

    private void removeDataAssociationComponents() {
        remove(cancelDataAssociationJButton);
        remove(dataAssociationJButton);
        remove(dataAssociationJLabel);
        remove(dataAssociationResultJLabel);
    }

    private void addMinMaxComponents() {
        add(minJLabel);
        add(minResultJLabel);
        add(minJTextField);
        add(maxJLabel);
        add(maxResultJLabel);
        add(maxJTextField);
    }

    private void removeMinMaxComponents() {
        remove(minJLabel);
        remove(minResultJLabel);
        remove(minJTextField);
        remove(maxJLabel);
        remove(maxResultJLabel);
        remove(maxJTextField);
    }

    private void addOneDimensionLengthComponents() {
        add(oneDimensionMinLengthJLabel);
        add(oneDimensionMinLengthJTextField);
        add(oneDimensionMinLengthResultJLabel);
        add(oneDimensionMaxLengthJLabel);
        add(oneDimensionMaxLengthJTextField);
        add(oneDimensionMaxLengthResultJLabel);
    }

    private void addTwoDimensionLengthComponents() {
        add(twoDimensionMinLengthJLabel);
        add(twoDimensionMinLengthJTextField);
        add(twoDimensionMinLengthResultJLabel);
        add(twoDimensionMaxLengthJLabel);
        add(twoDimensionMaxLengthJTextField);
        add(twoDimensionMaxLengthResultJLabel);
    }

    protected void addDataAssociationComponents() {
        add(dataAssociationJLabel);
        add(dataAssociationResultJLabel);
        add(dataAssociationJButton);
        add(cancelDataAssociationJButton);
    }

    public void setRowIndex(int rowIndex) {
        this.rowIndex = rowIndex;
        this.rowInfoJLabel.setText(String.format("第 [ %d ] 块", rowIndex + 1));
    }

    private void initComponents() {
        this.rowInfoJLabel = new JLabel(String.format("第 [ %d ] 块", rowIndex + 1));
        this.randomDataTypeResultJLabel = new JLabel(RandomDataType.INT.name());
        this.randomDataTypeJComboBox.setSelectedIndex(0);

        this.minJLabel = new JLabel("最小值：");
        this.minJTextField = new JTextField("0", 10);
        this.minResultJLabel = new JLabel("0");
        this.maxJLabel = new JLabel("最大值：");
        this.maxJTextField = new JTextField("1_000", 10);
        this.maxResultJLabel = new JLabel("1_000");

        this.deleteJButton = new JButton("删除");
        this.editJButton = new JButton("编辑");
        this.confirmJButton = new JButton("确定");
        initDataAssociationComponents();
        this.initArrayComponents = false;
        add(rowInfoJLabel);
        add(new JLabel("数据类型："));
        add(randomDataTypeResultJLabel);
        add(randomDataTypeJComboBox);
        add(minJLabel);
        add(minJTextField);
        add(minResultJLabel);
        add(maxJLabel);
        add(maxJTextField);
        add(maxResultJLabel);
        add(deleteJButton);
        add(editJButton);
        add(confirmJButton);
        add(dataAssociationJLabel);
        add(dataAssociationResultJLabel);
        add(dataAssociationJButton);
        add(cancelDataAssociationJButton);
    }

    public void initDataAssociationActionListener() {
        dataAssociationJButton.addActionListener(e -> {
            Object[] randomInDataConfigurationIndexCanBeAssociated =
                    configurationIndexAssociate.getRandomInDataConfigurationIndexCanBeAssociated().stream().filter(p -> p <= rowIndex).toArray();
            if (randomInDataConfigurationIndexCanBeAssociated.length == 0) {
                JOptionPane.showMessageDialog(null, "没有随机数据块可以和本随机数据块建立数据关联");
                return;
            }
            Object o = JOptionPane.showInputDialog(
                    this,
                    "数据关联",
                    "请选择本数据块的数据关联项",
                    QUESTION_MESSAGE,
                    null,
                    randomInDataConfigurationIndexCanBeAssociated,
                    randomInDataConfigurationIndexCanBeAssociated[0]
            );
            if (o == null) {
                dataAssociationResultJLabel.setText("无");
                this.cancelDataAssociationJButton.setVisible(false);
            } else {
                dataAssociationResultJLabel.setText(o.toString());
                this.cancelDataAssociationJButton.setVisible(true);
                this.dataAssociationJButton.setVisible(false);
            }
        });
        cancelDataAssociationJButton.addActionListener(e -> {
            dataAssociationResultJLabel.setText("无");
            dataAssociationJButton.setVisible(true);
            cancelDataAssociationJButton.setVisible(false);
        });
    }

    public void toEdited() {
        // 隐藏文本
        hideJLabel();
        // 显示输入框
        showJTextField();
        // 显示下拉框
        showJComboBox();
        this.editJButton.setVisible(false);
        this.confirmJButton.setVisible(true);
        dataAssociationComponentsToEdited();
    }

    public void dataAssociationComponentsToEdited() {
        this.dataAssociationJButton.setVisible(true);
        this.cancelDataAssociationJButton.setVisible(false);
    }

    public void confirmConfiguration() {
        // 显示文本
        showJLabel();
        // 隐藏输入框
        hideJTextField();
        // 隐藏下拉框
        hideJComboBox();
        this.editJButton.setVisible(true);
        this.confirmJButton.setVisible(false);
        dataAssociationComponentsExitEdited();
    }

    public void dataAssociationComponentsExitEdited() {
        this.dataAssociationJButton.setVisible(false);
        this.cancelDataAssociationJButton.setVisible(false);
    }

    private void hideJTextField() {
        minJTextField.setVisible(false);
        maxJTextField.setVisible(false);
        if (randomDataTypeJComboBox.getSelectedItem() == ARRAY) {
            oneDimensionMinLengthJTextField.setVisible(false);
            oneDimensionMaxLengthJTextField.setVisible(false);
            twoDimensionMinLengthJTextField.setVisible(false);
            twoDimensionMaxLengthJTextField.setVisible(false);
            printLengthJCheckBox.setEnabled(false);
        }
    }

    private void showJTextField() {
        RandomDataType selectedRandomDataType = (RandomDataType) randomDataTypeJComboBox.getSelectedItem();
        if (selectedRandomDataType != BOOLEAN) {
            if (selectedRandomDataType != RandomDataType.ARRAY || arrayElementRandomDataTypeJComboBox.getSelectedItem() != BOOLEAN) {
                minJTextField.setVisible(true);
                maxJTextField.setVisible(true);
            }
        }
        if (randomDataTypeJComboBox.getSelectedItem() == ARRAY) {
            oneDimensionMinLengthJTextField.setVisible(true);
            oneDimensionMaxLengthJTextField.setVisible(true);
            printLengthJCheckBox.setEnabled(true);
            if (arrayDimensionJComboBox.getSelectedItem() == ArrayDimension.TWO) {
                twoDimensionMinLengthJTextField.setVisible(true);
                twoDimensionMaxLengthJTextField.setVisible(true);
            }
        }
    }

    private void hideJLabel() {
        randomDataTypeResultJLabel.setVisible(false);
        minResultJLabel.setVisible(false);
        maxResultJLabel.setVisible(false);
        if (randomDataTypeJComboBox.getSelectedItem() == ARRAY) {
            arrayDimensionResultJLabel.setVisible(false);
            arrayElementResultJLabel.setVisible(false);
            oneDimensionMinLengthResultJLabel.setVisible(false);
            oneDimensionMaxLengthResultJLabel.setVisible(false);
            if (arrayDimensionJComboBox.getSelectedItem() == ArrayDimension.TWO) {
                twoDimensionMinLengthResultJLabel.setVisible(false);
                twoDimensionMaxLengthResultJLabel.setVisible(false);
            }
        }
    }

    private void showJLabel() {
        RandomDataType selectedRandomDataType = (RandomDataType) randomDataTypeJComboBox.getSelectedItem();
        randomDataTypeResultJLabel.setVisible(true);
        randomDataTypeResultJLabel.setText(selectedRandomDataType.name());
        if (selectedRandomDataType != BOOLEAN) {
            if (selectedRandomDataType != RandomDataType.ARRAY || arrayElementRandomDataTypeJComboBox.getSelectedItem() != BOOLEAN) {
                minResultJLabel.setVisible(true);
                maxResultJLabel.setVisible(true);
                minResultJLabel.setText(addUnderLine(removeRedundantUnderline(minJTextField.getText())));
                maxResultJLabel.setText(addUnderLine(removeRedundantUnderline(maxJTextField.getText())));
            }
        }
        if (selectedRandomDataType == ARRAY) {
            RandomDataType selectedArrayElementRandomDataType = (RandomDataType) arrayElementRandomDataTypeJComboBox.getSelectedItem();
            ArrayDimension selectedArrayDimension = (ArrayDimension) arrayDimensionJComboBox.getSelectedItem();
            arrayDimensionResultJLabel.setVisible(true);
            arrayDimensionResultJLabel.setText(selectedArrayDimension.name());
            arrayElementResultJLabel.setVisible(true);
            arrayElementResultJLabel.setText(selectedArrayElementRandomDataType.name());
            oneDimensionMinLengthResultJLabel.setVisible(true);
            oneDimensionMinLengthResultJLabel.setText(oneDimensionMinLengthJTextField.getText());
            oneDimensionMaxLengthResultJLabel.setVisible(true);
            oneDimensionMaxLengthResultJLabel.setText(oneDimensionMaxLengthJTextField.getText());
            if (selectedArrayDimension == ArrayDimension.TWO) {
                twoDimensionMinLengthResultJLabel.setVisible(true);
                twoDimensionMinLengthResultJLabel.setText(twoDimensionMinLengthJTextField.getText());
                twoDimensionMaxLengthResultJLabel.setVisible(true);
                twoDimensionMaxLengthResultJLabel.setText(twoDimensionMaxLengthJTextField.getText());
            }
        }
    }

    private void hideJComboBox() {
        randomDataTypeJComboBox.setVisible(false);
        if (randomDataTypeJComboBox.getSelectedItem() == ARRAY) {
            arrayDimensionJComboBox.setVisible(false);
            arrayElementRandomDataTypeJComboBox.setVisible(false);
        }
    }

    private void showJComboBox() {
        randomDataTypeJComboBox.setVisible(true);
        if (randomDataTypeJComboBox.getSelectedItem() == ARRAY) {
            arrayDimensionJComboBox.setVisible(true);
            arrayElementRandomDataTypeJComboBox.setVisible(true);
        }
    }

    public String getRandomDataType() {
        return randomDataTypeResultJLabel.getText();
    }

    public String getMin() {
        return removeRedundantUnderline(minResultJLabel.getText());
    }

    public String getMax() {
        return removeRedundantUnderline(maxResultJLabel.getText());
    }

    public int getMinLength() {
        try {
            return Integer.parseInt(oneDimensionMinLengthResultJLabel.getText());
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "最小长度请输入数字！");
            return 0;
        }
    }

    public int getMaxLength() {
        try {
            return Integer.parseInt(oneDimensionMaxLengthResultJLabel.getText());
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "最大长度请输入数字！");
            return 0;
        }
    }

    public ArrayDimension getArrayDimension() {
        return ArrayDimension.valueOf(arrayDimensionResultJLabel.getText());
    }

    public boolean isEdited() {
        return !editJButton.isVisible();
    }

    public boolean shouldPrintArrayLength() {
        return printLengthJCheckBox.isSelected();
    }

    public boolean isArray() {
        return randomDataTypeJComboBox.getSelectedItem() == RandomDataType.ARRAY;
    }

    public int getRowIndex() {
        return rowIndex;
    }

    public int dataAssociation() {
        String associationResult = dataAssociationResultJLabel.getText();
        return associationResult.equals("无") ? -1 : Integer.parseInt(associationResult);
    }

    public boolean isBoolean() {
        return randomDataTypeJComboBox.getSelectedItem() == BOOLEAN;
    }

    public RandomDataType getArrayElementRandomDataType() {
        return RandomDataType.valueOf(arrayElementResultJLabel.getText());
    }

    public int getTwoDimensionMinLength() {
        try {
            return Integer.parseInt(twoDimensionMinLengthResultJLabel.getText());
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "最小长度请输入数字！");
            return 0;
        }
    }

    public int getTwoDimensionMaxLength() {
        try {
            return Integer.parseInt(twoDimensionMaxLengthResultJLabel.getText());
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "最大长度请输入数字！");
            return 0;
        }
    }
}
